// proxySerive.js
import Toast from '../../miniprogram_npm/@vant/weapp/toast/toast';

const app = getApp()
const util = require("../../utils/util.js")

Page({

  /**
   * 页面的初始数据
   */
  data: {
    currentTab: '0',
    tablineStyle: '',
    serive:[],
    business: [],
    actionShow:false,
    step:1,
    // 添加
    add:{
      id:'',
      idError: "",
      name:'',
      type:'engineer',
      telphone:'',
      remark:''
    },
    // 删除
    oldId:'',
    newid:'',
    newidError:"",
    // 修改
    updateId:'',
    updateName:'',
    telphone:''
  },
  // 切换tab
  tabChoose: function (e) {
    var that = this;
    var cur = e.target.dataset.current;
    var type = cur == '0' ? 'engineer' : 'salesman';
    if (that.data.currentTab == cur) { return false; }
    else {
      that.setData({
        currentTab: cur
      })
    }
    that.tabLine();
    that.list(type);

  },
  // 切换tab滑块动画
  tabLine: function () {
    var query = wx.createSelectorQuery();
    var that = this;
    var currentTab = that.data.currentTab;
    query.select('.tab').boundingClientRect(function (rect) {
      that.setData({
        tablineStyle: "width:" + rect.width / 2 + "px;transform: translateX(" + rect.width * currentTab / 2 + "px);"
      })
    }).exec();
  },
  // 表单数值改变
  onChange: function (detail, event) {// detail 为当前输入的值
    var that = this;
    that.setData({
      [detail]: event.detail
    })
  },
  idChange: function (event) {
    this.onChange('add.id', event)
  },
  nameChange: function (event) {
    this.onChange('add.name', event)
  },
  remarkChange: function (event) {
    this.onChange('add.remark', event)
  },
  typeChange: function (event) {
    this.setData({
      ['add.type']: event.detail
    });
  },
  telChange: function (event) {
    this.setData({
      ['add.telphone']: event.detail
    });
  },
  updatetelChange:function(event) {
    this.setData({
      telphone: event.detail
    });
  },
  clearErr:function(){
    this.setData({
      ['add.idError']: ""
    })
  },
  // 添加人员
  add:function(){
    this.setData({
      actionShow:true,
      step:1
    })
  },
  actionClose: function () {
    this.setData({
      step: 1,
      actionShow: false,
      ['add.name']: "",
      ['add.id']: '',
      ['add.remark']: "",
      ['add.idError']: '',
      ['add.telphone']: '',
      oldId: '',
      newid: '',
      newidError: '',
      updateName: '',
      telphone: ''
    })
  },
  next: function () {
    var that = this;
    var add = that.data.add;
    var data = {id:add.id};
    if(add.id !==''){
      app.post('agent/verifyShopID', data).then((res) => {
        if (res.code == 200) {
          that.setData({
            step: 2
          })
        } else {
          that.setData({
            ['add.idError']: res.message
          })
        }
      }).catch((errMsg) => {
        console.log(errMsg);
        wx.showToast({
          title: "网络错误",
          icon: 'loading',
          duration: 2000
        })
      })
    }else {
      that.setData({
        ['add.idError']: '请输入用户ID'
      })
    }
  },
  addSave: function () {
    var that = this;
    var add = that.data.add;
    var data = {
      name:add.name,
      id:add.id,
      type: add.type,
      remark: add.remark,
      telphone:add.telphone
    };
    wx.showLoading({
      title: '加载中...',
    })
    app.post('agent/submitEmployee', data).then((res) => {
      wx.hideLoading();
      if (res.code == 200) {
        that.actionClose();
        that.onLoad();
      } else {
        Toast.loading(res.message)
      }
    }).catch((errMsg) => {
      console.log(errMsg);
      wx.hideLoading();
      wx.showToast({
        title: "网络错误",
        icon: 'loading',
        duration: 2000
      })
    })

    
  },
  // 删除人员
  deleteTap:function(e){
    var id = e.currentTarget.dataset.id;
    this.setData({
      actionShow: true,
      step: 0,
      oldId:id
    })
  },
  newidChange: function (event) {
    this.onChange('newid', event)
  },
  clearnewErr: function () {
    this.setData({
      newidError: ""
    })
  },
  deleteConfirm: function () {
    var that = this;
    var data = { id: that.data.newid };
    // 验证新ID
    app.post('agent/verifyShopID', data).then((res) => {
      if (res.code == 200) {
        that.setData({
          actionShow: false,
        })
        that.listdelete(that.data.oldId, that.data.newid)
      } else {
        that.setData({
          newidError: res.message
        })
      }
    }).catch((errMsg) => {
      console.log(errMsg);
      wx.showToast({
        title: "网络错误",
        icon: 'loading',
        duration: 2000
      })
    })
  },
  listdelete: function (id, newid) {
    var that = this;
    var type = that.data.currentTab == 0 ? "engineer" : "salesman";
    var data = {
      id:id,
      newId:newid,
      type:type
    };
    wx.showLoading({
      title: '加载中...',
    })
    app.post('agent/deleteEmployee', data).then((res) => {
      wx.hideLoading();
      if (res.code == 200) {
        wx.showToast({
          title: res.message,
          icon: 'success',
          duration: 2000
        })
        that.actionClose();
        that.onLoad();
      } else {
        wx.showToast({
          title: res.message,
          icon: 'loading',
          duration: 2000
        })
      }
    }).catch((errMsg) => {
      console.log(errMsg);
      wx.hideLoading();
      wx.showToast({
        title: "网络错误",
        icon: 'loading',
        duration: 2000
      })
    })
  },
  // 修改用户信息
  updateTap:function(e){
    var id = e.currentTarget.dataset.id;
    var list = this.data.list;
    var updateName, telphone;
    list.forEach(item=>{
      if(item.id==id){
        updateName = item.name,
        telphone = item.telphone
      }
    })
    this.setData({
      actionShow: true,
      step: 3,
      updateId: id,
      updateName: updateName,
      telphone: telphone
    })
  },
  updateNameChange: function (event) {
    this.onChange('updateName', event)
  },
  updateConfirm:function(){
    var that = this;
    var data = {
      id: that.data.updateId,
      name: that.data.updateName,
      telphone:that.data.telphone
    };
    wx.showLoading({
      title: '加载中...',
    })
    app.post('agent/updateEmpName', data).then((res) => {
      wx.hideLoading();
      if (res.code == 200) {
        wx.showToast({
          title: res.message,
          icon: 'success',
          duration: 2000
        })
        that.actionClose();
        that.onLoad();
      } else {
        wx.showToast({
          title: res.message,
          icon: 'loading',
          duration: 2000
        })
      }
    }).catch((errMsg) => {
      console.log(errMsg);
      wx.hideLoading();
      wx.showToast({
        title: "网络错误",
        icon: 'loading',
        duration: 2000
      })
    })
  },
  // 管理店铺
  seriveShop: function (e) {
    var active = this.data.currentTab;
    var myid = e.currentTarget.dataset.id;
    wx.navigateTo({
      url: '../../pages/proxySeriveManage/proxySeriveManage?active=' + active + '&myid=' + myid,
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var type = this.data.currentTab == '0' ? 'engineer' : 'salesman';
    this.tabLine();
    this.list(type);
    // this.list('salesman');
    wx.stopPullDownRefresh()
  },
  list:function(type){
    var that = this;
    var data = {type:type};
    app.post('agent/getAgentEmployee', data).then((res) => {
      if (res.code == 200) {
          that.setData({
            list: res.list
          })
      } 
    }).catch((errMsg) => {
      console.log(errMsg);
      wx.showToast({
        title: "网络错误",
        icon: 'loading',
        duration: 2000
      })
    })
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.onLoad();
  },
  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    // 请求最新数据
    this.onLoad();
    this.actionClose();
  }
})